package com.spic.business.accident.interevents.management.service.impl;

import java.util.List;

import com.spic.business.accident.interevents.management.mapper.InternalIncidentReportMapper;
import com.spic.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.spic.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.spic.business.accident.interevents.management.domain.InternalEventsImplementationPersonnel;
import com.spic.business.accident.interevents.management.domain.InternalIncidentReport;
import com.spic.business.accident.interevents.management.service.IInternalIncidentReportService;

/**
 * 内部事件管理Service业务层处理
 * 
 * @author 任翔
 * @date 2021-12-01
 */
@Service
public class InternalIncidentReportServiceImpl implements IInternalIncidentReportService 
{
    @Autowired
    private InternalIncidentReportMapper internalIncidentReportMapper;
    /**
     * 查询内部事件管理
     * 
     * @param id 内部事件管理主键
     * @return 内部事件管理
     */
    @Override
    public InternalIncidentReport selectInternalIncidentReportById(Integer id)
    {
        return internalIncidentReportMapper.selectInternalIncidentReportById(id);
    }

    /**
     * 查询内部事件管理列表
     * 
     * @param internalIncidentReport 内部事件管理
     * @return 内部事件管理
     */
    @Override
    public List<InternalIncidentReport> selectInternalIncidentReportList(InternalIncidentReport internalIncidentReport)
    {
        return internalIncidentReportMapper.selectInternalIncidentReportList(internalIncidentReport);
    }

    /**
     * 新增内部事件管理
     * 
     * @param internalIncidentReport 内部事件管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertInternalIncidentReport(InternalIncidentReport internalIncidentReport)
    {
        internalIncidentReport.setCreateTime(DateUtils.getNowDate());
        int rows = internalIncidentReportMapper.insertInternalIncidentReport(internalIncidentReport);
        insertInternalEventsImplementationPersonnel(internalIncidentReport);
        return rows;
    }

    /**
     * 修改内部事件管理
     * 
     * @param internalIncidentReport 内部事件管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateInternalIncidentReport(InternalIncidentReport internalIncidentReport)
    {
        internalIncidentReport.setUpdateTime(DateUtils.getNowDate());
        internalIncidentReportMapper.deleteInternalEventsImplementationPersonnelByInternalEvents(internalIncidentReport.getId());
        insertInternalEventsImplementationPersonnel(internalIncidentReport);
        return internalIncidentReportMapper.updateInternalIncidentReport(internalIncidentReport);
    }

    /**
     * 批量删除内部事件管理
     * 
     * @param ids 需要删除的内部事件管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteInternalIncidentReportByIds(Integer[] ids)
    {
        internalIncidentReportMapper.deleteInternalEventsImplementationPersonnelByIds(ids);
        return internalIncidentReportMapper.deleteInternalIncidentReportByIds(ids);
    }

    /**
     * 删除内部事件管理信息
     * 
     * @param id 内部事件管理主键
     * @return 结果
     */
    @Override
    public int deleteInternalIncidentReportById(Integer id)
    {
        internalIncidentReportMapper.deleteInternalEventsImplementationPersonnelByInternalEvents(id);
        return internalIncidentReportMapper.deleteInternalIncidentReportById(id);
    }

    /**
     * 新增事件落实人员信息
     * 
     * @param internalIncidentReport 内部事件管理对象
     */
    public void insertInternalEventsImplementationPersonnel(InternalIncidentReport internalIncidentReport)
    {
        List<InternalEventsImplementationPersonnel> internalEventsImplementationPersonnelList = internalIncidentReport.getInternalEventsImplementationPersonnelList();
        Integer id = internalIncidentReport.getId();
        if (StringUtils.isNotNull(internalEventsImplementationPersonnelList))
        {
            List<InternalEventsImplementationPersonnel> list = new ArrayList<InternalEventsImplementationPersonnel>();
            for (InternalEventsImplementationPersonnel internalEventsImplementationPersonnel : internalEventsImplementationPersonnelList)
            {
                internalEventsImplementationPersonnel.setInternalEvents((long)id);
                list.add(internalEventsImplementationPersonnel);
            }
            if (list.size() > 0)
            {
                internalIncidentReportMapper.batchInternalEventsImplementationPersonnel(list);
            }
        }
    }
}
